<?php

namespace app\adminmini\service;


use app\adminmini\model\Seat;
use think\facade\Db;

class SeatService extends BaseService
{
    public function __construct()
    {
        $this->model = new Seat();
    }

    public function edit()
    {
        $data = request()->param();
        $data['store_id'] = $this->userId();


        $result = $this->model->edit($data, $error);

        Db::name('customer_seat_house')->where('seat_id', $result)->delete();

        return message('操作成功', true);
    }


    /**
     * @param $result
     * @param $roleIds
     * @return void
     */
    public function insertlabel($result, $roleIds)
    {
        if (!empty($roleIds)) {
            $list = [];
            foreach ($roleIds as $val) {
                $data = [
                    'seat_id' => $result,
                    'house_id' => $val,
                ];
                $list[] = $data;
            }
            Db::name('customer_seat_house')->insertAll($list);
        }
    }


    /**
     * 处理数据
     * @param $arr
     * @return bool|string
     */
    public function arr_to_str($arr)
    {
        $t = '';
        foreach ($arr as $v) {
            $temp[] = $v;
        }
        foreach ($temp as $v) {
            $t .= $v . ',';
        }
        $t = substr($t, 0, -1); // 利用字符串截取函数消除最后一个逗号
        return $t;
    }


    /**
     * 获取时期
     * @return array
     */
    public function time()
    {
        $hours_add_time = array(
            'hours_add_time' => [
                1 => '00',
                2 => '01',
                3 => '02',
                4 => '03',
                5 => '04',
                6 => '05',
                7 => '06',
                8 => '07',
                9 => '08',
                10 => '09',
                11 => '10',
                12 => '11',
                13 => '12',
                14 => '13',
                15 => '14',
                16 => '15',
                17 => '16',
                18 => '17',
                19 => '18',
                20 => '19',
                21 => '20',
                22 => '21',
                23 => '22',
                24 => '23',

            ],
            'hours_end_time' => [
                1 => '00',
                2 => '01',
                3 => '02',
                4 => '03',
                5 => '04',
                6 => '05',
                7 => '06',
                8 => '07',
                9 => '08',
                10 => '09',
                11 => '10',
                12 => '11',
                13 => '12',
                14 => '13',
                15 => '14',
                16 => '15',
                17 => '16',
                18 => '17',
                19 => '18',
                20 => '19',
                21 => '20',
                22 => '21',
                23 => '22',
                24 => '23',
                25 => '24',

            ],
            'time_add' => [
                0 => '00',
                1 => '01',
                2 => '02',
                3 => '03',
                4 => '04',
                5 => '05',
                6 => '06',
                7 => '07',
                8 => '08',
                9 => '09',
                10 => '10',
                11 => '11',
                12 => '12',
                13 => '13',
                14 => '14',
                15 => '15',
                16 => '16',
                17 => '17',
                18 => '18',
                19 => '19',
                20 => '20',
                21 => '21',
                22 => '22',
                23 => '23',
                24 => '24',
                25 => '25',
                26 => '26',
                27 => '27',
                28 => '28',
                29 => '29',
                30 => '30',
                31 => '31',
                32 => '32',
                33 => '33',
                34 => '34',
                35 => '35',
                36 => '36',
                37 => '37',
                38 => '38',
                39 => '39',
                40 => '40',
                41 => '41',
                42 => '42',
                43 => '43',
                44 => '44',
                45 => '45',
                46 => '46',
                47 => '47',
                48 => '48',
                49 => '49',
                50 => '50',
                51 => '51',
                52 => '52',
                53 => '53',
                54 => '54',
                55 => '55',
                56 => '56',
                57 => '57',
                58 => '58',
                59 => '59',
            ],
        );

        return $hours_add_time;
    }


    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];


        // 名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }

        $map[] = ['store_id', '=', $this->userId()];
        $map[] = ['mark', '=', 1];
        $list = Db::name('customer_seat')->where($map)->page(PAGE, PERPAGE)
            ->select();

        foreach ($list as $i => $info) {
            $happy = Db::name('mini_happy')->where('id', $info['happy_id'])->find();


            $info['haypp_name'] = $happy['name'];
            $info['num'] = $happy['num'];

            $moth = Db::name('customer_method')->where('id', $info['method_id'])->find();
            $hours_time = $this->time();
            $hours_add_time = $hours_time['hours_add_time'];
            $hours_end_time = $hours_time['hours_end_time'];
            $time_add = $hours_time['time_add'];
            $info['add_time'] = $hours_add_time[$moth['add_time'] + 1] . ':' . $time_add[$moth['add_hour']];
            $info['end_time'] = $hours_end_time[$moth['end_time'] + 1] . ':' . $time_add[$moth['end_hour']];
            $list[$i] = $info;
        }
        $count = Db::name('customer_seat')
            ->where($map)->page(PAGE, PERPAGE)
            ->count();

        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }
}